package com.mall.dw.util;

import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Darren
 * @date 2022-10-14 14:02
 * Description：
 */
@Data
public class TimeUtil {
    public static final String FORMAT1 = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT2 = "yyyy-MM-dd";
    public static final String FORMAT3 = "yyyy年MM月dd日 HH时mm分ss秒";
    public static final String FORMAT4 = "yyyy年MM月dd日";
    public static final String UNIT_HOUR = "hour";
    public static final String UNIT_DAY = "day";

    /**
     * 当前时间增加N Unit
     *
     * @param unit
     * @param num
     * @return
     */
    public static String beforeTime(String unit, Integer num) {
        // 1小时为单位
        long times = 3600000;
        if (unit.equalsIgnoreCase(UNIT_HOUR)) {
            times = times * num;
        } else if (unit.equalsIgnoreCase(UNIT_DAY)) {
            times = times * 24 * num;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT1);
        return simpleDateFormat.format(new Date(System.currentTimeMillis() - times));
    }
}
